"""
Case Type   : Audit
Case Name   : 验证创建条件字符处理函数检查成功
Create At   : 2025/04/01
Owner       : liu-tong-8848
Description :
              1.审计检查验证创建条件字符处理函数
              sys.argv[1] = $GAUSSHOME/lib/postgresql/libog_query.so
              sys.argv[2] = "select hex(weight_string('abc' as binary(2)));
                             select hex(weight_string('abc' as char(2) LEVEL 1 ));
                             select hex(weight_string('abc' as char(2) LEVEL 1 DESC));
                             select hex(weight_string('abc' as char(2) LEVEL 1 REVERSE));
                             SELECT UNCOMPRESS(COMPRESS('2022-05-12 10:30:00'));
                             SELECT HEX(COMPRESS('2022-05-12 10:30:00'));
                             SELECT name_const('abc', 123);
                             select char_bool('-0.0100abc');
                             select varchar_bool('-0.0100abc');
                             select text_bool('-0.01abc');
                             SELECT HEX(UNHEX('1267'));
                             SELECT TO_BASE64('to_base64');
                             select ord('Ŷ1111');
                             SELECT FROM_BASE64('YWJj');
                             SELECT 'abc' like binary 'a%' as result;
                             SELECT 'abc' like 'a' as result;
                             select make_set(1|4, 'one', 'two', NULL, 'four');
                             select field('ceshi','wo','ceshi','disange');
                             select left('abcde', 2);
                             select convert('a' using 'utf8');
                             SELECT char_length(B'101');
                             select chara(77,77.3,'77.3','78.8',78.8);
                             SELECT bin('你好');
                             SELECT 'str' rlike '[ac]' AS RESULT;
                             SELECT octet_length('中文');
                             SELECT locate('ing', 'string');
                             select hex('abc'::bytea);
                             SELECT bit_length(b'010');"
              # -*- coding: utf-8 -*-
              import sys
              import ctypes
              class OgQueryParseResult(ctypes.Structure):
                  _fields_ = [
                      ("parse_tree_json", ctypes.c_char_p),
                      ("err_msg", ctypes.c_char_p),
                      ("is_passed", ctypes.c_bool)
                  ]
              libpg_query = ctypes.CDLL(sys.argv[1])
              libpg_query.raw_parser_opengauss_dolphin.restype = OgQueryParseResult
              libpg_query.raw_parser_opengauss_dolphin.argtypes = [ctypes.c_char_p]
              sql_bytes = sys.argv[2].encode('utf-8')
              result = libpg_query.raw_parser_opengauss_dolphin(sql_bytes)
              print(sql_bytes)
              print(result.parse_tree_json)
              print(result.err_msg)
              print(result.is_passed)
Expect      :
              1.成功
History     :
"""

import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Audit(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.log.info('==Opengauss_Function_Grammar_Audit_Case045开始==')
        self.python_file_name = "test_audit.py"
        self.db_user_node = Node(node='PrimaryDbUser')

    def test_add_dimension(self):
        text = '----step1: 审计检查验证创建条件字符处理函数 expect: 成功----'
        self.log.info(text)
        init_order_1 = "echo '" \
                       "# -*- coding: utf-8 -*-\n" \
                       "import sys\n" \
                       "import ctypes\n" \
                       "\n" \
                       "class OgQueryParseResult(ctypes.Structure):\n" \
                       "    _fields_ = [\n" \
                       "        (\"parse_tree_json\", ctypes.c_char_p),\n" \
                       "        (\"err_msg\", ctypes.c_char_p),\n" \
                       "        (\"is_passed\", ctypes.c_bool)\n" \
                       "    ]\n" \
                       "\n" \
                       "' > " + self.python_file_name
        init_order_2 = "echo \"" \
                       "libpg_query = ctypes.CDLL(sys.argv[1])\n" \
                       "libpg_query.raw_parser_opengauss_dolphin.restype = OgQueryParseResult\n" \
                       "libpg_query.raw_parser_opengauss_dolphin.argtypes = [ctypes.c_char_p]\n" \
                       "\n" \
                       "sql_bytes = sys.argv[2].encode('utf-8')\n" \
                       "result = libpg_query.raw_parser_opengauss_dolphin(sql_bytes)\n" \
                       "print(sql_bytes)\n" \
                       "print(result.parse_tree_json)\n" \
                       "print(result.err_msg)\n" \
                       "print(result.is_passed)\n" \
                       "\" >> " + self.python_file_name
        self.log.info(init_order_1)
        self.log.info(init_order_2)
        init_result_1 = self.db_user_node.sh(init_order_1).result()
        init_result_2 = self.db_user_node.sh(init_order_2).result()
        self.assertNotIn('ERROR', init_result_1, "执行失败")
        self.assertNotIn('ERROR', init_result_2, "执行失败")

        self.log.info("cat " + self.python_file_name)
        cat_result = self.db_user_node.sh("cat " + self.python_file_name).result()
        self.log.info(cat_result)

        test_so_file_name = "$GAUSSHOME/lib/postgresql/libog_query.so"
        test_sql = "select hex(weight_string('abc' as binary(2))); " \
                   + "select hex(weight_string('abc' as char(2) LEVEL 1 )); " \
                   + "select hex(weight_string('abc' as char(2) LEVEL 1 DESC)); " \
                   + "select hex(weight_string('abc' as char(2) LEVEL 1 REVERSE)); " \
                   + "SELECT UNCOMPRESS(COMPRESS('2022-05-12 10:30:00')); " \
                   + "SELECT HEX(COMPRESS('2022-05-12 10:30:00')); " \
                   + "SELECT name_const('abc', 123); " \
                   + "select char_bool('-0.0100abc'); " \
                   + "select varchar_bool('-0.0100abc'); " \
                   + "select text_bool('-0.01abc'); " \
                   + "SELECT HEX(UNHEX('1267')); " \
                   + "SELECT TO_BASE64('to_base64'); " \
                   + "select ord('Ŷ1111'); " \
                   + "SELECT FROM_BASE64('YWJj'); " \
                   + "SELECT 'abc' like binary 'a%' as result; " \
                   + "SELECT 'abc' like 'a' as result; " \
                   + "select make_set(1|4, 'one', 'two', NULL, 'four'); " \
                   + "select field('ceshi','wo','ceshi','disange'); " \
                   + "select left('abcde', 2); " \
                   + "select convert('a' using 'utf8'); " \
                   + "SELECT char_length(B'101'); " \
                   + "select chara(77,77.3,'77.3','78.8',78.8); " \
                   + "SELECT bin('你好'); " \
                   + "SELECT 'str' rlike '[ac]' AS RESULT; " \
                   + "SELECT octet_length('中文'); " \
                   + "SELECT locate('ing', 'string'); " \
                   + "select hex('abc'::bytea); " \
                   + "SELECT bit_length(b'010');"
        test_order = "python3 " + self.python_file_name + " " + test_so_file_name + " \"" + test_sql + "\""
        self.log.info(test_order)
        source_order = "source " + macro.DB_ENV_PATH + ";"
        self.log.info(source_order)
        test_result = self.db_user_node.sh(source_order + test_order).result()
        self.log.info(test_result)
        self.assertIn('None', test_result.splitlines()[-2], "执行失败")
        self.assertIn('True', test_result.splitlines()[-1], "执行失败")

    def tearDown(self):
        text = '----step2: 清理环境 expect: 成功----'
        self.log.info(text)
        drop_python_order = "rm -rf " + self.python_file_name
        drop_python_result = self.db_user_node.sh(drop_python_order).result()
        self.log.info(drop_python_result)
        self.assertNotIn('ERROR', drop_python_result, "执行失败")
        self.log.info('==Opengauss_Function_Grammar_Audit_Case045完成==')
